home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / readfs.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  15KB  |  580 lines

  1. /* readfs - read a MINIX file system    Author: Paul Polderman */
  2.  
  3. /* Command: readfs - read and extract a MINIX filesystem.
  4.  *
  5.  * Syntax:  readfs [-li] block-special [directory]
  6.  *
  7.  * Flags: -l:    Extract files and dirs and produce a mkfs-listing on stdout
  8.  *       -i:    Information only: give the listing, but do not extract files.
  9.  *
  10.  * Examples: readfs /dev/fd1        # extract all files from /dev/fd1.
  11.  *          readfs -i /dev/hd2        # see what's on /dev/hd2.
  12.  *          readfs -l /dev/at0 rootfs    # extract and list the filesystem
  13.  *                     # of /dev/at0 and put the tree
  14.  *                     # in the directory `rootfs'.
  15.  *
  16.  *   Readfs reads a MINIX filesystem and extracts recursively all directories
  17.  * and files, and (optionally) produces a mkfs-listing of them on stdout.
  18.  * The root directory contents are placed in the current directory, unless
  19.  * a directory is given as argument, in which case the contents are put there.
  20.  * Readfs tries to restore the attributes (mode/uid/gid/time) of the files
  21.  * extracted to those of the original files.
  22.  * Special files are created as ordinary files, but the mkfs-listing
  23.  * enables mkfs to restore them to original.
  24.  */
  25.  
  26. #include <sys/types.h>
  27. #include <fcntl.h>
  28. #include <limits.h>
  29. #include <unistd.h>
  30. #include <stdio.h>
  31.  
  32. #include <minix/config.h>
  33. #include <minix/const.h>
  34. #include <minix/type.h>
  35. #include "../fs/const.h"
  36. #include "../fs/type.h"
  37. #include "../fs/buf.h"
  38. #include "../fs/super.h"
  39.  
  40.  
  41. #undef printf            /* Definition used only in the kernel */
  42.  
  43. #define block_nr long        /* To allow big filesystems even if FS
  44.              * doesn't. */
  45.  
  46.  
  47. /* Compile with -I/user0/ast/minix
  48.  * (i.e. the directory containing the MINIX system sources)
  49.  *
  50.  *    Author: Paul Polderman (polder@cs.vu.nl) April 1987
  51.  */
  52.  
  53. char verbose = 0;        /* give a mkfs-listing of the filesystem */
  54.  /* And extracts its contents. */
  55. char noaction = 0;        /* just give a mkfs-listing, do not extract
  56.              * files. */
  57.  
  58. struct super_block sb;
  59. char pathname[1024];
  60.  
  61. main(argc, argv)
  62. int argc;
  63. char **argv;
  64. {
  65.   switch (argc) {
  66.       case 2:
  67.     pathname[0] = '\0';
  68.     readfs(argv[1], pathname);
  69.     break;
  70.       case 3:
  71.     if (argv[1][0] == '-') {
  72.         get_flags(&argv[1][1]);
  73.         pathname[0] = '\0';
  74.         readfs(argv[2], pathname);
  75.     } else {
  76.         strcpy(pathname, argv[2]);
  77.         readfs(argv[1], pathname);
  78.     }
  79.     break;
  80.       case 4:
  81.     if (argv[1][0] == '-') {
  82.         get_flags(&argv[1][1]);
  83.         strcpy(pathname, argv[3]);
  84.         readfs(argv[2], pathname);
  85.         break;
  86.     }            /* else fall through .. */
  87.       default:
  88.     fprintf(stderr, "Usage: %s [-li] <special> [dirname]\n", argv[0]);
  89.     exit(1);
  90.   }
  91.   exit(0);
  92. }
  93.  
  94. get_flags(flags)
  95. register char *flags;
  96. {
  97.   while (*flags) {
  98.     switch (*flags) {
  99.         case 'L':
  100.         case 'l':    verbose = 1;    break;
  101.         case 'I':
  102.         case 'i':
  103.         noaction = 1;
  104.         verbose = 1;
  105.         break;
  106.         default:
  107.         fprintf(stderr, "Bad flag: %c\n", *flags);
  108.         break;
  109.     }
  110.     flags++;
  111.   }
  112. }
  113.  
  114. #define    zone_shift    (sb.s_log_zone_size)    /* zone to block ratio */
  115.  
  116. readfs(special_file, directory)
  117. char *special_file, *directory;
  118. /* Readfs: opens the given special file (with MINIX filesystem),
  119.  * and extracts its contents into the given directory.
  120.  */
  121. {
  122.   d_inode root_inode;
  123.   int special;
  124.   off_t super_b;
  125.  
  126.   umask(0);
  127.  
  128.   /* Open the special file */
  129.   if ((special = open(special_file, O_RDONLY)) < 0) {
  130.     fprintf(stderr, "cannot open %s\n", special_file);
  131.     return;
  132.   }
  133.  
  134.   /* Read the superblock */
  135.   super_b = (off_t) SUPER_BLOCK *(off_t) BLOCK_SIZE;
  136.   if (lseek(special, super_b, SEEK_SET) != super_b) {
  137.     fprintf(stderr, "cannot seek to superblock\n");
  138.     return;
  139.   }
  140.   if (read(special, (char *) &sb, sizeof(struct super_block))
  141.       != sizeof(struct super_block)) {
  142.     fprintf(stderr, "cannot read superblock\n");
  143.     return;
  144.   }
  145.  
  146.   /* Is it really a MINIX filesystem ? */
  147.   if (sb.s_magic != SUPER_MAGIC) {
  148.     fprintf(stderr, "%s is not a valid MINIX filesystem\n",
  149.         special_file);
  150.     return;
  151.   }
  152.  
  153.   /* Fetch the inode of the root directory */
  154.   if (get_inode(special, (ino_t) ROOT_INODE, &root_inode) < 0) {
  155.     fprintf(stderr, "cannot get inode of root directory\n");
  156.     return;
  157.   }
  158.  
  159.   /* Print number of blocks and inodes */
  160.   if (verbose) printf("boot\n%ld %d\n",
  161.            (block_nr) sb.s_nzones << zone_shift, sb.s_ninodes);
  162.  
  163.   /* Extract (recursively) the root directory */
  164.   dump_dir(special, &root_inode, directory);
  165. }
  166.  
  167. /* Different type of blocks:    (used in routine get_block for caching) */
  168.  
  169. #define    B_INODE        0    /* Cache #0 is the inode cache */
  170. #define    B_INDIRECT    1    /* Cache #1 is the (dbl) indirect block cache */
  171. #define    B_DATA        2    /* No cache for data blocks (only read once) */
  172.  
  173. int get_inode(fd, inum, ip)
  174. int fd;
  175. ino_t inum;
  176. d_inode *ip;
  177.  
  178. /* Get inode `inum' from the MINIX filesystem. (Uses the inode-cache) */
  179. {
  180.   struct buf bp;
  181.   block_nr block;
  182.   block_nr ino_block;
  183.   unshort ino_offset;
  184.   int r;
  185.  
  186.   /* Calculate start of i-list */
  187.   block = SUPER_BLOCK + 1 + sb.s_imap_blocks + sb.s_zmap_blocks;
  188.  
  189.   /* Calculate block with inode inum */
  190.   ino_block = ((inum - 1) / INODES_PER_BLOCK);
  191.   ino_offset = ((inum - 1) % INODES_PER_BLOCK);
  192.   block += ino_block;
  193.  
  194.   /* Fetch the block */
  195.   if (get_block(fd, block, &bp, B_INODE) == 0) {
  196.     bcopy(&bp.b_inode[ino_offset], ip, sizeof(d_inode));
  197.     return(0);
  198.   }
  199.  
  200.   /* Oeps, foutje .. */
  201.   fprintf(stderr, "cannot find inode %d\n", inum);
  202.   return(-1);
  203. }
  204.  
  205. static int indent = 0;        /* current indent (used for mkfs-listing) */
  206.  
  207. dump_dir(special, ip, directory)
  208. int special;
  209. d_inode *ip;
  210.  
  211. char *directory;
  212. /* Make the given directory (if non-NULL),
  213.  * and recursively extract its contents.
  214.  */
  215. {
  216.   register dir_struct *dp;
  217.   register int n_entries;
  218.   register char *name;
  219.   block_nr b = 0;
  220.   d_inode dip;
  221.   struct buf bp;
  222.  
  223.   if (verbose) {
  224.     show_info(directory, ip, "");
  225.     indent++;
  226.   }
  227.   if (!noaction && *directory) {
  228.     /* Try to make the directory if not already there */
  229.     if (mkdir(directory) != 0 || chdir(directory) < 0) {
  230.         fprintf(stderr, "mkdir %s failed\n", directory);
  231.         return;
  232.     }
  233.   }
  234.   for (name = directory; *name; name++)    /* Find end of pathname */
  235.     ;
  236.   *name++ = '/';        /* Add trailing slash */
  237.  
  238.   n_entries = (int) (ip->i_size / (off_t) sizeof(dir_struct));
  239.   while (n_entries > 0) {
  240.  
  241.     /* Read next block of the directory */
  242.     if (get_fileblock(special, ip, b, &bp) < 0) return;
  243.     dp = &bp.b_dir[0];
  244.     if (b++ == (block_nr) 0) {
  245.         dp += 2;    /* Skip "." and ".." */
  246.         n_entries -= 2;
  247.     }
  248.  
  249.     /* Extract the files/directories listed in the block */
  250.     while (n_entries-- > 0 && dp < &bp.b_dir[NR_DIR_ENTRIES]) {
  251.         if (dp->d_inum != (ino_t) 0) {
  252.             if (get_inode(special, dp->d_inum, &dip) < 0) {
  253.                 /* Bad luck */
  254.                 dp++;
  255.                 continue;
  256.             }
  257.  
  258.             /* Add new pathname-component to `pathname'. */
  259.             strncpy(name, dp->d_name, NAME_MAX);
  260.             name[NAME_MAX] = '\0';
  261.  
  262.             /* Call the right routine */
  263.             if ((dip.i_mode & I_TYPE) == I_DIRECTORY)
  264.                 dump_dir(special, &dip, name);
  265.             else
  266.                 dump_file(special, &dip, name);
  267.         }
  268.         dp++;        /* Next entry, please. */
  269.     }
  270.   }
  271.   *--name = '\0';        /* Restore `pathname' to what it was. */
  272.   if (!noaction && *directory) {
  273.     chdir("..");        /* Go back up. */
  274.     restore(directory, ip);    /* Restore mode/owner/accesstime */
  275.   }
  276.   if (verbose) {
  277.     do_indent(--indent);    /* Let mkfs know we are done */
  278.     printf("$\n");        /* with this directory. */
  279.   }
  280. }
  281.  
  282. dump_file(special, ip, filename)
  283. int special;
  284. d_inode *ip;
  285. char *filename;
  286. /* Extract given filename from the MINIX-filesystem,
  287.  * and store it on the local filesystem.
  288.  */
  289. {
  290.   dir_struct *dp;
  291.   int file;
  292.   block_nr b = 0;
  293.   struct buf bp;
  294.   off_t size;
  295.  
  296.   if (verbose) show_info(filename, ip, pathname);
  297.  
  298.   if (noaction) return(0);
  299.  
  300.   if (access(filename, 0) == 0) {
  301.     /* Should not happen, but just in case .. */
  302.     fprintf(stderr, "Will not create %s: file exists\n", filename);
  303.     return(-1);
  304.   }
  305.   if ((file = creat(filename, (ip->i_mode & ALL_MODES))) < 0) {
  306.     fprintf(stderr, "cannot create %s\n", filename);
  307.     return(-1);
  308.   }
  309.  
  310.   /* Don't try to extract /dev/hd0 */
  311.   if ((ip->i_mode & I_TYPE) == I_REGULAR) {
  312.     size = ip->i_size;
  313.     while (size > (off_t) 0) {
  314.         /* Get next block of file */
  315.         if (get_fileblock(special, ip, b++, &bp) < 0) {
  316.             close(file);
  317.             return(-1);
  318.         }
  319.  
  320.         /* Write it to the file */
  321.         if (size > (off_t) BLOCK_SIZE)
  322.             write(file, bp.b_data, BLOCK_SIZE);
  323.         else
  324.             write(file, bp.b_data, (int) size);
  325.  
  326.         size -= (off